/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * POS_SearchByCategory.java
 *
 * Created on Apr 3, 2011, 10:24:33 PM
 */

package POS_Source;

import DTO.Category;
import POS_Controller.POSController;
import java.util.ArrayList;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 * this class renders category navigation view
 * @author Spence
 * @see POS_Source
 */
public class POS_SearchByCategory extends javax.swing.JPanel {

     private POSController controller;

    /**
     * constructor for this class
     * @param control
     */
    public POS_SearchByCategory(POSController control) {
        initComponents();
        controller = control;
        customInit();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        CategoryPane = new javax.swing.JScrollPane();
        CategoryTree = new javax.swing.JTree();
        SearchButton = new javax.swing.JButton();
        TitleLabel = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        CategoryTree.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        CategoryTree.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        CategoryTree.setLargeModel(true);
        CategoryTree.setMinimumSize(new java.awt.Dimension(220, 130));
        CategoryPane.setViewportView(CategoryTree);

        SearchButton.setFont(new java.awt.Font("Copperplate Gothic Bold", 2, 18)); // NOI18N
        SearchButton.setText("Search");
        SearchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SearchButtonActionPerformed(evt);
            }
        });

        TitleLabel.setFont(new java.awt.Font("Copperplate Gothic Bold", 2, 42)); // NOI18N
        TitleLabel.setText("Categories at DigiStore");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(537, Short.MAX_VALUE)
                .add(SearchButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 251, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(CategoryPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 500, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(TitleLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 580, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(208, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(212, 212, 212)
                        .add(SearchButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 94, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(CategoryPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 524, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(TitleLabel)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
/**
 * this method handles search button event
 * @param evt
 */
    private void SearchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SearchButtonActionPerformed
        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) CategoryTree.getLastSelectedPathComponent();
        if (selectedNode != null){
            Category selectedCategory = (Category) selectedNode.getUserObject();
            if(selectedCategory.getCtid() != 0){
                DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) selectedNode.getParent();
                Category parentCategory = (Category) parentNode.getUserObject();
            }
            else
                   return;
            controller.searchByCategory(selectedCategory.getCtid());
        }
        else{
            // Printe error message
        }
        
        

    }//GEN-LAST:event_SearchButtonActionPerformed
/**
 * this method overrides default view rendering 
 */
    private void customInit(){
        ArrayList<Category> list = controller.getCategories();
        DefaultMutableTreeNode root = processTree(list, list.get(0));
        CategoryTree = new JTree(root);
        CategoryTree.setRowHeight(35);
        CategoryTree.setRootVisible(Boolean.FALSE);
        CategoryPane.setViewportView(CategoryTree);
    }
/**
 * this method builds a category tree recursively
 * @param list
 * @param rootNode
 * @return
 */
    private DefaultMutableTreeNode processTree(ArrayList<Category> list, Category rootNode){

        DefaultMutableTreeNode node = new DefaultMutableTreeNode(rootNode);
        DefaultMutableTreeNode child;
            for(int i = 0; i < list.size() ; i++){
                Category nodeSpecifier = list.get(i);

                if (nodeSpecifier.getPctid() == rootNode.getCtid()){
                    child = processTree(list, nodeSpecifier);
                    node.add(child);
                }
            }
        return node;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane CategoryPane;
    private javax.swing.JTree CategoryTree;
    private javax.swing.JButton SearchButton;
    private javax.swing.JLabel TitleLabel;
    // End of variables declaration//GEN-END:variables

}
